package com.hemj.fileInfo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hemj.common.exception.ServiceException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hemj.fileInfo.mapper.FileInfoLogMapper;
import com.hemj.fileInfo.domain.FileInfoLog;
import com.hemj.fileInfo.service.IFileInfoLogService;
import com.hemj.common.utils.IdGenerator.IdGeneratorFactory;
import com.hemj.fileInfo.vo.FileInfoLogVo;
import com.hemj.fileInfo.bo.FileInfoLogBo;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文件日志Service业务层处理
 * 
 * @author hemj
 * @date 2024-10-21
 */
@Service
public class FileInfoLogServiceImpl extends ServiceImpl<FileInfoLogMapper, FileInfoLog> implements IFileInfoLogService
{
    @Autowired
    private IdGeneratorFactory idGeneratorFactory;
    
    /**
         * 验证参数
         */
    private void validateData(FileInfoLog fileInfoLog){
    }

    /**
     * 保存数据
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public FileInfoLog saveCascade(FileInfoLogVo jsonObject){
        FileInfoLog fileInfoLog = new FileInfoLog();
        BeanUtils.copyProperties(jsonObject,fileInfoLog);
        validateData(fileInfoLog);
        fileInfoLog.setId(idGeneratorFactory.getId(FileInfoLog.class.getSimpleName()));
        this.save(fileInfoLog);
        return fileInfoLog;
    }
    /**
     * 编辑数据
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public FileInfoLog updateCascade(FileInfoLogVo jsonObject){
        FileInfoLog fileInfoLog = this.getById(jsonObject.getId());;
        BeanUtils.copyProperties(jsonObject,fileInfoLog);
        validateData(fileInfoLog);
        this.updateById(fileInfoLog);
        return fileInfoLog;
    }

    /**
     * 初始化查询条件
     */
    public LambdaQueryWrapper<FileInfoLog> initQueryWrapper(FileInfoLogVo jsonObject){
        LambdaQueryWrapper<FileInfoLog> queryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(jsonObject.getId())) {
        }
        if (!ObjectUtils.isEmpty(jsonObject.getParentId())) {
            queryWrapper.eq(FileInfoLog::getParentId, jsonObject.getParentId());
        }
        if (!ObjectUtils.isEmpty(jsonObject.getAncestors())) {
            queryWrapper.eq(FileInfoLog::getAncestors, jsonObject.getAncestors());
        }
        if (!ObjectUtils.isEmpty(jsonObject.getName())) {
            queryWrapper.like(FileInfoLog::getName, jsonObject.getName());
        }
        if (!ObjectUtils.isEmpty(jsonObject.getType())) {
            queryWrapper.eq(FileInfoLog::getType, jsonObject.getType());
        }
        if (!ObjectUtils.isEmpty(jsonObject.getCreateBy())) {
        }
        if (!ObjectUtils.isEmpty(jsonObject.getCreateTime())) {
        }
        if (!ObjectUtils.isEmpty(jsonObject.getUpdateBy())) {
        }
        if (!ObjectUtils.isEmpty(jsonObject.getUpdateTime())) {
        }
        if (!ObjectUtils.isEmpty(jsonObject.getUrl())) {
            queryWrapper.eq(FileInfoLog::getUrl, jsonObject.getUrl());
        }
        if (!ObjectUtils.isEmpty(jsonObject.getSize())) {
            queryWrapper.eq(FileInfoLog::getSize, jsonObject.getSize());
        }
        if (!ObjectUtils.isEmpty(jsonObject.getSeqNo())) {
            queryWrapper.eq(FileInfoLog::getSeqNo, jsonObject.getSeqNo());
        }
        queryWrapper.orderByDesc(FileInfoLog::getId);
        return queryWrapper;
    }

    /**
     * list查询
     */
    public List<FileInfoLogBo> queryList(FileInfoLogVo jsonObject) {
        List<FileInfoLog> fileInfoLogs = this.list(initQueryWrapper(jsonObject));
        List<FileInfoLogBo> bos = new ArrayList<>();
        for (FileInfoLog fileInfoLog:fileInfoLogs) {
            FileInfoLogBo fileInfoLogBo = new FileInfoLogBo();
            BeanUtils.copyProperties(fileInfoLog,fileInfoLogBo);
            bos.add(fileInfoLogBo);
        }
        return bos;
    }

    /**
     * 统计
     */
    public long countList(FileInfoLogVo jsonObject) {
        return this.count(initQueryWrapper(jsonObject));
    }

    /**
     * 获取单个数据
     */
    public FileInfoLogBo getOneById(Long id){
        FileInfoLog fileInfoLog = this.getById(id);
        FileInfoLogBo fileInfoLogBo = new FileInfoLogBo();
        BeanUtils.copyProperties(fileInfoLog,fileInfoLogBo);
        return fileInfoLogBo;
    }

}
